package com.esl.coordinator.command;

import java.io.IOException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.esl.coordinator.exception.STiTEQATMELIntegrationException;
import com.esl.coordinator.util.ESLCommandUtil;
import com.esl.coordinator.util.Hex;

public class LoadLCDInternalPatternSelectCommand extends Command {
	static final Logger LOG = LoggerFactory
			.getLogger(LoadLCDInternalPatternSelectCommand.class);

	public static final String UART_LCD_INTERNAL_PATTERN_SELECT_LEAD_CODE = "CC";
	public static final String UART_LCD_INTERNAL_PATTERN_SELECT_LENGTH = "05";
	public static final String UART_LCD_INTERNAL_PATTERN_SELECT_COMMAND_TYPE = "62";

	private static final int EXPECTED_RESPONSE_BYTES = 8;

	private String macAddress;
	private int patternArea;
	private int patternNumber;

	public LoadLCDInternalPatternSelectCommand(String macAddress,
			int patternArea, int patternNumber, CommandReceiver commandClient) {
		super(commandClient, EXPECTED_RESPONSE_BYTES);
		this.macAddress = macAddress;
		this.patternArea = patternArea;
		this.patternNumber = patternNumber;
	}

	@Override
	public String[] buildCommand() throws STiTEQATMELIntegrationException {
		StringBuffer messageString = new StringBuffer();
		messageString.append(UART_LCD_INTERNAL_PATTERN_SELECT_LEAD_CODE);
		messageString.append(UART_LCD_INTERNAL_PATTERN_SELECT_LENGTH);
		messageString.append(UART_LCD_INTERNAL_PATTERN_SELECT_COMMAND_TYPE);

		String lowMacByte = ESLCommandUtil.getMacLowByte(getmacAddress());
		String highMacByte = ESLCommandUtil.getMacHighByte(getmacAddress());

		messageString.append(lowMacByte);
		messageString.append(highMacByte);

		messageString.append(Hex.toHexString(getPatternArea()));
		messageString.append(Hex.toHexString(getPatternNumber()));

		return new String[] { messageString.toString() };
	}

	@Override
	public boolean isCommandSuccessful() {
		if (getCommandResponse() != null && getCommandResponse().length == 1) {
			if (getCommandResponse()[0].endsWith("FF")) {
				return true;
			}
		}
		return false;
	}

	public String getmacAddress() {
		return macAddress;
	}

	public void setmacAddress(String macAddress) {
		this.macAddress = macAddress;
	}

	public int getPatternArea() {
		return patternArea;
	}

	public void setPatternArea(int patternArea) {
		this.patternArea = patternArea;
	}

	public int getPatternNumber() {
		return patternNumber;
	}

	public void setPatternNumber(int patternNumber) {
		this.patternNumber = patternNumber;
	}

	public static void main(String[] args) {
		Command testCmd;
		try {
			testCmd = STiTEQATMELCommandFactory.getInstance()
					.createLoadLCDInternalPatternSelectCommand(
							"08-8E",
							0,
							4,
							SocketCommandReceiver.getInstance("192.168.0.2",
									8088));
			CommandInvoker.sendCommand(testCmd);
			testCmd.isCommandSuccessful();
		} catch (STiTEQATMELIntegrationException e) {
			LOG.error("Error in test.", e);
		} catch (IOException e) {
			LOG.error("Error in test.", e);
		}
	}
}
